<template>
  <div class="search_results">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="(item, index) in list"
        :key="index"
        :title="item.title"
      />
    </van-list>
  </div>
</template>

<script>
import { getSearchResults } from '@/api/search.js/'
export default {
  name: 'searchResults',
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      perPage: 20
    }
  },
  props: {
    q: {
      type: String,
      required: true
    },
    searchText: {
      type: String,
      required: true
    }
  },
  methods: {
    async onLoad() {
      const { data } = await getSearchResults({
        page: this.page, // 页码
        per_page: this.perPage, // 每页大小
        q: this.q // 搜索关键词
      })
      const { results } = data.data
      this.list.push(...results)
      // 设置加载状态结束
      this.loading = false
      // 判断加载是否完毕
      if (results.length) {
        // 更新获取下一页数据的页码
        this.page++
      } else {
        // 没有数据了 将加载状态设置结束 不再load
        this.finished = true
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
